package com.shujia.spark.sql

import org.apache.spark.sql.{DataFrame, SaveMode, SparkSession}

object Demo6SparkOnHie {
  def main(args: Array[String]): Unit = {
    val spark: SparkSession = SparkSession
      .builder()
      .config("spark.sql.shuffle.partitions", "1")
      //开启动hive元数据支持，用于读取hive中的表
      .enableHiveSupport()
      .getOrCreate()

    /**
    * 可以直接使用hive中的表
      *
    * sql 的语法完全兼容hive
    */

    val df: DataFrame = spark.sql(
      """
        |select clazz,count(1) from student group by clazz
      """.stripMargin)
    df.write
      .format("csv")
      .mode(SaveMode.Overwrite)
      .save("/data/clazzNum")

    /**
      * 打包上传到服务器运行
      * spark-submit --class com.shujia.spark.sql.Demo6SparkOnHie --master yarn-client spark-1.0-SNAPSHOT.jar
      *
      */
  }
}
